/**
 * 客户资源录入脚本支持
 */
var clientKey = null;
var modifydata = null;
var saveAfterClose = false;

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 键盘事件
	$( "body" ).bind( "keyup", bodyKeyupHandler );

	// 初始化
	init_city( "province", "city" );
	button_bind();

	// 修改数据设置
	if( modifydata )
		setClientData( modifydata );
} );

//--------------------------------------------------------------------------
//
//	consultant_client.php
//
//--------------------------------------------------------------------------

function button_bind()
{
	$( "#btnNewClient" ).bind( "click", function()
	{
		setClientData();
	} );

	$( "#btnSelect" ).bind( "click", function()
	{
		parent.clienter.setCallback( clientSelectedHandler );
		parent.clienter.reset();
		parent.clienter.open();
	} );

	$( "#btnSubmit" ).bind( "click", function()
	{
		submit_form();
	} );

	$( "#url" ).parent().parent().hide();
	$( "#source" ).change( function()
	{
		var tr = $( "#url" ).parent().parent();
		if( $( this ).val() == "pmvk5vdc6ity" )
		{
			tr.show();
		}
		else
		{
			tr.hide();
		}
	} );

	$( "#btnAddCategory" ).click( function( e )
	{
		parent.adder.setCallback( submit_newCategory );
		parent.adder.open( "添加新客户类别", "类别名称：", "<em><b>说明</b>：请尽量精简保持在5个汉字以内，并且不要重复添加相同的类别名称。</em>" );
	} );

	$( "#btnAddSource" ).click( function( e )
	{
		parent.adder.setCallback( submit_newsource );
		parent.adder.open( "添加新客户来源渠道", "渠道名称：", "<em><b>说明</b>：请尽量精简保持在5个汉字以内，并且不要重复添加相同的渠道名称。</em>" );
	} );

	$( "#btnAddQuest" ).click( function()
	{
		quest_add( $( "#quest_list" ), "", "" );
	} );

	// 添加客户跟踪
	$( "#btnAddTracking" ).click( exec_tracking );

	// 弹出学员资料编辑窗口
	$( "#btnStudent" ).click( edit_student );

	// 重复检测
	$( "#phone" ).focusout( { id: "phone" }, check_client );
	$( "#qq" ).focusout( { id: "qq" }, check_client );
	$( "#email" ).focusout( { id: "email" }, check_client );
}

function check_client( e )
{
	switch( e.data.id ) {
		case "phone":
		{
			chk_client_repeat( $( "#phone" ).val(), '', '', clientKey, phone_repeat );
			break;
		}
		case "qq":
		{
			chk_client_repeat( '', $( "#qq" ).val(), '', clientKey, qq_repeat );
			break;
		}
		case "email":
		{
			chk_client_repeat( '', '', $( "#email" ).val(), clientKey, email_repeat );
			break;
		}
	}
}

function phone_repeat( v )
{
	$( "#phone" ).parent().find( "p[rel='errnote']" ).text( v.err == 1 ? "该号码已存在，勿重复添加客户资源。（登记人：" + v.reg + " - 负责人：" + v.chg + "）" : "" );
}
function qq_repeat( v )
{
	$( "#qq" ).parent().find( "p[rel='errnote']" ).text( v.err == 1 ? "该QQ已存在，勿重复添加客户资源。（登记人：" + v.reg + " - 负责人：" + v.chg + "）" : "" );
}
function email_repeat( v )
{
	$( "#email" ).parent().find( "p[rel='errnote']" ).text( v.err == 1 ? "该邮箱已存在，勿重复添加客户资源。（登记人：" + v.reg + " - 负责人：" + v.chg + "）" : "" );
}
function exec_tracking()
{
	if( empty( clientKey ) )
	{
		infopad.err( "请先指定目标客户，再添加跟踪情况。" );
		return;
	}

	var url = "consultant_client_tracking.php?key=" + clientKey;
	var title = "追加跟踪记录";
	var id = "consultant_client_tracking";

	parent.create_model( url, title, id, 8999 );
	parent.open_model( id );
}

function edit_student()
{
	if( empty( clientKey ) )
	{
		infopad.err( "请先指定目标客户，再编辑学员资料。" );
		return;
	}

	var url = "consultant_student.php?pkey=" + clientKey;
	var title = "编辑学员资料";
	var id = "consultant_student_" + clientKey;

	parent.create_model( url, title, id );
	parent.open_model( id );
}

//--------------------------------------------------------------------------
//
//	数据提交
//
//--------------------------------------------------------------------------

function submit_newCategory( arr )
{
	submitter.post( "consultant_category_add_go.php", {
		name: arr[ 0 ],
		explain: arr[ 1 ]
	}, function( d )
	{
		if( d.err == 0 )
		{
			infopad.msg( d.msg );

			$( "#class" ).append(
					"<option value='" + d.key + "' title='" + d.explain + "'>" + d.name + "</option>" );
			$( "#class" ).val( d.key );
		}
		else
		{
			infopad.err( d.msg );
		}
	}, "json" );
}

function submit_newsource( arr )
{
	submitter.post( "consultant_source_add_go.php", {
		name: arr[ 0 ],
		explain: arr[ 1 ]
	}, function( d )
	{
		if( d.err == 0 )
		{
			infopad.msg( d.msg );

			$( "#source" ).append(
					"<option value='" + d.key + "' title='" + d.explain + "'>" + d.name + "</option>" );
			$( "#source" ).val( d.key );
		}
		else
		{
			infopad.err( d.msg );
		}
	}, "json" );
}

function clientSelectedHandler( data )
{
	if( typeof ( data ) == "undefined" )
		return;

	setClientData( data );
}

function setClientData( data )
{
	// 客户资料
	if( typeof ( data ) == "object" && data.nickname )
	{
		clientKey = data.key;

		$( "#nickname" ).val( data.nickname );
		$( "#invalid" ).prop( "checked", data.invalid == 1 );
		$( "#category" ).val( data.category );
		$( "#source" ).val( data.source );
		$( "#url" ).val( data.url );
		$( "#keyword" ).val( data.keyword );

		// 根据来源设置，决定是否显示商桥网址
		var tr = $( "#url" ).parent().parent();
		if( data.source == "pmvk5vdc6ity" )
		{
			tr.show();
		}
		else
		{
			tr.hide();
		}

		// 设置问卷地址列表
		quest_init( $( "#quest_list" ), $.parseJSON( data.quests ) );

		$( "#province" ).val( data.province );
		update_selected( data.city );

		$( "#address" ).val( data.address );
		$( "#phone" ).val( data.phone );
		$( "#email" ).val( data.email );
		$( "#qq" ).val( data.qq );
		$( "#ip" ).val( data.ip );
		$( "#desc" ).html( data.desc );
	}
	else
	{
		clientKey = null;

		$( "#nickname" ).val( "" );
		$( "#invalid" ).prop( "checked", false );
		$( "#category > option:first" ).attr( "selected", true );
		$( "#source > option:first" ).attr( "selected", true );
		$( "#province > option:first" ).attr( "selected", true );
		$( "#city > option:first" ).attr( "selected", true );
		$( "#url" ).val( "" );
		$( "#keyword" ).val( "" );
		$( "#quest_list" ).html( "<em class=\"caption\">请点击左侧“+”添加新问卷地址</em>" );
		$( "#address" ).val( "" );
		$( "#phone" ).val( "" );
		$( "#email" ).val( "" );
		$( "#qq" ).val( "" );
		$( "#ip" ).val( "" );
		$( "#desc" ).html( "" );

		$( "#url" ).parent().parent().hide();
	}
}

function submit_form()
{
	var data = {};

	// 客户资料
	data.key = clientKey;
	data.nickname = $( "#nickname" ).val();
	data.invalid = $( "#invalid" ).prop( "checked" ) ? 1 : 0;
	data.category = $( "#category" ).val();
	data.source = $( "#source" ).val();
	data.url = $( "#url" ).val();
	data.keyword = $( "#keyword" ).val();
	data.quests = JSON.stringify( quest_collect( $( "#quest_list" ) ) );
	data.province = $( "#province" ).val();
	data.city = $( "#city" ).val();
	data.address = $( "#address" ).val();
	data.phone = $( "#phone" ).val();
	data.email = $( "#email" ).val();
	data.qq = $( "#qq" ).val();
	data.ip = $( "#ip" ).val();
	data.desc = htmlfilter( $( "#desc" ).html() );

	submitter.post( "consultant_client_go.php", data, function( d )
	{
		if( d.err == 0 )
		{
			infopad.msg( d.msg );

			if( d.type == "add" )
			{
				clientKey = d.key;
			}
			else
			{
				if( saveAfterClose )
					close_this_model();
			}
		}
		else
		{
			infopad.err( d.msg, d.delay );
		}
	}, "json" );
}

//--------------------------------------------------------------------------
//
//	methods
//
//--------------------------------------------------------------------------

function bodyKeyupHandler( e )
{
	if( e.ctrlKey && e.which == 13 )
	{
		submit_form();
	}
}
